package com.itheima.controller;


import cn.itheima.health.entity.QueryPageBean;
import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.*;
import cn.itheima.health.service.OrderSettingService;
import cn.itheima.health.service.SetmealService;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;



    @RequestMapping("/importOrderSettings.do")
    public Result importOrderSettings(@RequestParam("excelFile")MultipartFile file){
        try {
            //   解析 excel    ---mysql    t_ordersetting
            //   List<Ordersetting>  --->mysql
            List<OrderSetting> list = new ArrayList<OrderSetting>();
              //  获取excel所有的数据
            List<String[]> readExcel = POIUtils.readExcel(file);
            for (String[] strings : readExcel) {
                OrderSetting orderSetting =new OrderSetting();
                Date date = DateUtils.parseString2Date(strings[0], "yyyy/MM/dd");
                orderSetting.setOrderDate(date);
                orderSetting.setNumber(Integer.parseInt(strings[1]));
                list.add(orderSetting);
            }
            orderSettingService.add(list);
            return  new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }




//    @RequestMapping("/findPage.do")
//    public Result findPage(@RequestBody QueryPageBean queryPageBean){
//        try {
//
//            Map map = setmealService.findPage(queryPageBean);
//            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
//        }
//
//    }

    @RequestMapping("/updateNumbersByDate.do")
    public Result updateNumbersByDate(@RequestParam("date") String  date,@RequestParam("num") Integer num){
        try {
            orderSettingService.updateNumbersByDate(date,num);
            return  new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.EDIT_ORDERSETTING_FAIL);
        }

    }


    @RequestMapping("/findOrderSettingByDate.do")
    public Result findOrderSettingByDate(@RequestParam("year") Integer year,@RequestParam("month") Integer month){
        try {
            String  beginDate = year+"-"+month+"-1";
            String  endDate = year+"-"+month+"-31";
           List<Map> mapList =  orderSettingService.findOrderSettingByDate(beginDate,endDate);
           //  2020-8-1        2020-8-28 29 30 31----》List<Map>
//           select  *   from   xxx
//           where  orderDate between  2000-2-1  and  2000-2-31
            return  new Result(true, MessageConstant.QUERY_ORDER_SUCCESS,mapList);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }

    }
}
